package top.yvyan.guettable.moreFun;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import top.yvyan.guettable.Gson.ProgramModel;
import top.yvyan.guettable.R;
import top.yvyan.guettable.adapter.PlannedCourseAdapter;
import top.yvyan.guettable.bean.PlannedCourseBean;
import top.yvyan.guettable.data.MoreData;
import top.yvyan.guettable.data.TokenData;
import top.yvyan.guettable.service.fetch.StaticService;
import top.yvyan.guettable.util.AppUtil;
import top.yvyan.guettable.util.BackgroundUtil;

import static com.xuexiang.xui.XUI.getContext;

public class PlannedCoursesActivity extends BaseFuncActivity {


    @Override
    protected void childInit() {
        setTitle(getResources().getString(R.string.moreFun_plan_courses));
        setShowMore(false);
        openUpdate();
        AppUtil.reportFunc(getApplicationContext(), getString(R.string.moreFun_plan_courses));

    }

    @Override
    protected void showContent() {
        baseSetContentView(R.layout.activity_planned_courses);
        RecyclerView recyclerView = findViewById(R.id.planned_course_info_recycler_view);
        if (BackgroundUtil.isSetBackground(this)) {
            recyclerView.setBackgroundColor(ContextCompat.getColor(this, R.color.writeTransparentT));
        }
        List<PlannedCourseBean> plannedCourseBeans = MoreData.getPlannedCourseBeans();
        if (plannedCourseBeans.isEmpty()) {
            showEmptyPage();
        } else {
            PlannedCourseAdapter plannedCourseAdapter = new PlannedCourseAdapter(plannedCourseBeans);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerView.setAdapter(plannedCourseAdapter);
        }
    }

    public List<PlannedCourseBean> parseModel(List<ProgramModel.Module> modules,String parentName) {
        List<PlannedCourseBean> plannedCourseBeans = new ArrayList<>();
        for (ProgramModel.Module module : modules) {
            String classType="";
            if(!parentName.isEmpty()) {
                classType+=parentName+"-";
            }
            classType += module.classType;
            for (ProgramModel.Module.Course course : module.courseList) {
                if (course.rank==null) continue;
                PlannedCourseBean plannedCourseBean = new PlannedCourseBean(course.courseName, course.credits.toString(), course.rank, module.typeId.toString(), classType);
                plannedCourseBeans.add(plannedCourseBean);
            }
            plannedCourseBeans.addAll(parseModel(module.children,classType));
        }
        return plannedCourseBeans;
    }

    @Override
    public int updateData(TokenData tokenData) {
        ProgramModel programModel = StaticService.getProgramModel(this, tokenData.getbkjwTestCookie());
        if (programModel != null) {
            List<PlannedCourseBean> plannedCourseBeans = parseModel(programModel.moduleList,"");
            for (ProgramModel.Module.Course course : programModel.outerCourseList) {
                if (course.rank==null) continue;
                PlannedCourseBean plannedCourseBean = new PlannedCourseBean(course.courseName, course.credits.toString(), course.rank, "计划外", "计划外课程");
                plannedCourseBeans.add(plannedCourseBean);
            }
            update=true;
            MoreData.setPlannedCourseBeans(plannedCourseBeans);
            return 5;
        } else {
            return 1;
        }
    }
}
